package com.ps.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import com.ps.constant.MessageConstant;
import com.ps.constant.StatusConstant;
import com.ps.dto.CategoryDTO;
import com.ps.dto.CategoryPageQueryDTO;
import com.ps.entity.Category;
import com.ps.entity.Medicine;
import com.ps.entity.Setmeal;
import com.ps.exception.DeletionNotAllowedException;
import com.ps.mapper.CategoryMapper;
import com.ps.mapper.MedicineMapper;
import com.ps.mapper.SetmealMapper;
import com.ps.result.PageResult;
import com.ps.service.CategoryService;
import com.ps.service.MedicineService;
import com.ps.service.SetmealMedicineService;
import com.ps.service.SetmealService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.ps.constant.MessageConstant.CATEGORY_IS_NULL;


@Service
@Slf4j
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    private final CategoryMapper categoryMapper;
    private final MedicineService medicineService;
    private final SetmealService setmealService;

    public void saveCategory(CategoryDTO categoryDTO) {
        if (categoryDTO == null) {
            throw new IllegalArgumentException(CATEGORY_IS_NULL);
        }
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        save(category);
    }


    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //基于pagehelper分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    public void deleteById(Long id) {
        long  medicineCount= medicineService
                .lambdaQuery().eq(Medicine::getCategoryId, id).count();
        if (medicineCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_Medicine);
        }
        // 检查分类是否关联了药品组合
        long setmealCount =setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, id).count();
        if (setmealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 删除分类数据
        removeById(id);
    }


    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        updateById(category);
    }

    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        updateById(category);
    }


    public List<Category> list(Integer type) {
        return lambdaQuery()
                .eq(Category::getStatus, 1)
                .eq(type!=null,Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime).list();
    }
}
